"""
@name : 回合对战游戏
@author : FanMY_71
@projectname: Pycharm练习
@Time: 2022/7/7-14:48
"""
import random


class User():
    def __init__(self, name, sex, blood=100, equip=None, room=None):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.equip = equip
        self.room = room

    def fight(self, other):
        """
        随机产生10-15的伤害，伤害和血量为1:1
        防御装备可以获得50%免伤
        :param other:对手用户
        :return:
        """
        ATK = random.randint(5, 10)
        input("按Enter发起进攻")
        print(f"{self.name}向{other.name}发起进攻！".center(20, "*"))
        if other.equip is not None:
            other.blood -= (ATK // 2)
            if other.blood <= 0:
                self.room = None
                other.room = None
                print(f"造成了{ATK}点伤害，{other.name}现在血量为{other.blood}")
                return 0
            print(f"造成了{ATK}点伤害，{other.name}现在血量为{other.blood}")
            return 1
        else:
            other.blood -= ATK
            if other.blood <= 0:
                self.room = None
                other.room = None
                print(f"造成了{ATK}点伤害，{other.name}现在血量为{other.blood}")
                return 0
            print(f"造成了{ATK}点伤害，{other.name}现在血量为{other.blood}")
            return 1

    def pay_equip(self):
        self.equip = "防弹衣"
        print(f"恭喜{self.name}成功购买传说装备————{self.equip}")
        print(f"该装备对于受到的攻击对自身只产生一半的效果")

    def rem_equip(self):
        print(f"{self.equip}已从{self.name}身上卸除")
        self.equip = None


class Room():
    def __init__(self, name):
        self.name = name
        self.men_room = []
        # 存放用户的实例对象
        self.ex_men_room = []

    def join_room(self, man):
        if man.room is None and len(self.men_room) < 2:
            self.men_room.append(man.name)
            self.ex_men_room.append(man)
            man.room = self.name
            print(f"{man.name}成功加入{self.name}房间")
        else:
            print(f"{man.name}已加入其他房间或者{self.name}房间已满")


def menu():
    print("1.创建角色".center(20, "*"))
    print("2.创建房间".center(20, "*"))
    print("3.加入房间".center(20, "*"))
    print("4.开始战斗".center(20, "*"))
    print("5.装备服务".center(20, "*"))
    print("6.退出战斗".center(20, "*"))
    print("7.查询已存在的用户和房间".center(20, "*"))


if __name__ == '__main__':
    # 存放实例对象
    ex_name_list = []
    # 存放对象名字
    name_list = []
    ex_room_list = []
    room_list = []
    while True:
        menu()
        choose = input("请输入你的选择：")
        # 创建角色
        if choose == "1":
            username = input("请输入想要创建的用户名：")
            if username not in name_list:
                sex = input("请输入性别：(f-女，m-男)")
                # 定义实例对象
                ex_user = User(username, sex)
                ex_name_list.append(ex_user)
                name_list.append(ex_user.name)

                print(f"成功创建{ex_user.name}")
            else:
                print("该用户已创建")
        # 创建房间
        elif choose == "2":
            room_name = input("请输入想要创建的房间名：")
            if room_name not in room_list:
                ex_room = Room(room_name)
                # 加入实例化对象
                ex_room_list.append(ex_room)
                room_list.append(ex_room.name)
                print(f"成功创建房间{ex_room.name}")
            else:
                print("该房间已存在")
        # 加入房间
        elif choose == "3":
            join_man_num = int(input("请选择要加入房间的人的序号"))
            join_room_num = int(input("请选择要加入的房间号"))
            if len(ex_room_list[join_room_num].men_room) < 2:
                ex_room_list[join_room_num].join_room(ex_name_list[join_man_num])
            else:
                print("亲，人满了，请换一个房间哦~")
        # 开始战斗
        elif choose == "4":
            view_room_num = int(input('请选择你要参观的战斗房间的序号：'))
            if len(ex_room_list[view_room_num].men_room) < 2:
                print("你想要参观的房间没有满人，请等待一会")
            else:
                print("开始战斗！！")
                flag = 1
                # 记录回合数
                sum = 1
                # 随机抽取先手
                a = [0, 1]
                firstly = random.randint(0, 1)
                a.remove(firstly)
                secondly = a[0]
                print(
                    f"{ex_room_list[view_room_num].men_room[0]}正在和{ex_room_list[view_room_num].men_room[1]}战斗！".center(
                        20, "*"))
                while flag:
                    print(f"进入第{sum}回合".center(20,"#"))
                    flag = ex_room_list[view_room_num].ex_men_room[firstly].fight(
                        ex_room_list[view_room_num].ex_men_room[secondly])
                    if ex_room_list[view_room_num].ex_men_room[secondly].blood <= 0:
                        print(f"恭喜{ex_room_list[view_room_num].ex_men_room[firstly].name}获得胜利")
                        ex_room_list[view_room_num].men_room = []
                        ex_room_list[view_room_num].ex_men_room = []
                        break
                    flag = ex_room_list[view_room_num].ex_men_room[secondly].fight(
                        ex_room_list[view_room_num].ex_men_room[firstly])
                    if ex_room_list[view_room_num].ex_men_room[firstly].blood <= 0:
                        print(f"恭喜{ex_room_list[view_room_num].ex_men_room[secondly].name}获得胜利")
                        ex_room_list[view_room_num].men_room = []
                        ex_room_list[view_room_num].ex_men_room = []
                        break
                    sum += 1
        # 装备服务
        elif choose == "5":
            print("欢迎来到坑'猪'店")
            print("pay.购买装备\tremove.卸载")
            men_option = int(input("请选择享受服务的人的序号"))
            option = input("请您选择想要的服务(按q退出服务)")
            while 1:
                if option.lower() == "pay":
                    ex_name_list[men_option].pay_equip()
                    break
                elif option.lower() == "remove":
                    ex_name_list[men_option].rem_equip()
                    break
                elif option.lower() == "q":
                    print("欢迎下次光临")
                    break
                else:
                    print("选项输入有误！请重新输入")
                    continue
        # 退出战斗
        elif choose == "6":
            print("期待和你的下次相遇")
            break
        # 查询已存在的用户和房间
        elif choose == "7":
            print("有以下用户".center(30, "*"))
            for i, j in enumerate(ex_name_list):
                print(f"{i}.姓名：{j.name}  性别：{j.sex}")
            print("有以下房间".center(30, "*"))
            for i, j in enumerate(ex_room_list):
                print(f"{i}.房间名：{j.name}  在房中的人：{j.men_room}")
        else:
            print("选择输入有误！请重新输入！")